package com.aube.interact.fighter;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by huyaonan on 16/11/22.
 */
public class GamePrizeProgress extends View {

    private Paint mPaint, mPaintBorder, mPaintBg;
    private LinearGradient mLinearGradient;
    private float ratio=0;

    public GamePrizeProgress(Context context) {
        this(context, null);
    }

    public GamePrizeProgress(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GamePrizeProgress(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mPaint = new Paint();
        mPaint.setAntiAlias(true);

        mPaintBg = new Paint();
        mPaintBg.setAntiAlias(true);
        mPaintBg.setColor(0xa0000000);

        mPaintBorder = new Paint();
        mPaintBorder.setAntiAlias(true);
        mPaintBorder.setStyle(Paint.Style.STROKE);
        mPaintBorder.setColor(Color.RED);
        mPaintBorder.setStrokeWidth(2);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int width = getWidth();
        int height = getHeight();

        RectF mBorderRect = new RectF(0, 0, width, height);
        RectF mProgress = new RectF(0, 0, width*ratio, height);

        //画圆角矩形底色
        canvas.drawRoundRect(mBorderRect, 5, 5, mPaintBg);
        //画圆角矩形边框
        canvas.drawRoundRect(mBorderRect, 5, 5, mPaintBorder);

        if(mLinearGradient == null) {
            mLinearGradient = new LinearGradient(0, height/2, width, height, new int[]{Color.RED, Color.YELLOW}, null, Shader.TileMode.CLAMP);
            mPaint.setShader(mLinearGradient);
        }
        //画进度条
        canvas.drawRoundRect(mProgress, 5, 5, mPaint);
    }

    public void updateProgress(float ratio) {
        if(ratio > 1)
            ratio = 1f;
        this.ratio = ratio;
        invalidate();
    }

}
